import Vue from 'vue'

import * as filter from './utils/filter.js'
Vue.prototype.$const = filter

import { Upload,AliExcelImp } from '@/api/ctrls.js'

import Cookies from 'js-cookie'
import { formatDate } from './utils/index.js'
import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element, { Loading } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log

import Vue2OrgTree from 'vue-tree-color'
Vue.component('vue2-ort-tree', Vue2OrgTree);

import JsonExcel from 'vue-json-excel'
Vue.component('downloadExcel', JsonExcel)
import UploadImageList from '@/components/Upload/UploadImageList.vue'
Vue.component('UploadImageList',UploadImageList)

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.prototype.$submitFile = function ({ file }, callback) {
  const loadingInstance = Loading.service({
    lock: true,
    text: 'Loading',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  try {
    const formData = new FormData()
    formData.append('file', file)
    Upload(formData).then(({ data, success }) => {
      if (success) {
        callback({
          url: data
        })
      }
    }).finally(() => {
      loadingInstance.close()
    })
  } catch (e) {
    console.log('上传失败信息', e);
    loadingInstance.close()
  }
}

Vue.prototype.$AliExcelImp = function ({ file }, callback) {
  const loadingInstance = Loading.service({
    lock: true,
    text: 'Loading',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
  try {
    const formData = new FormData()
    formData.append('file', file)
    AliExcelImp(formData).then(({ data, success,msg }) => {
      callback({ data, success,msg })
    }).finally(() => {
      loadingInstance.close()
    })
  } catch (e) {
    console.log('上传失败信息', e);
    loadingInstance.close()
  }
}

import * as filters from '@/utils/filter.js'
for (const key of Object.keys(filters)) {
  Vue.filter(key, filters[key])
}

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
